package com.zyw.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@SuppressWarnings("rawtypes")
public class Time
{
  public static String getNowTime()
  {
    return 
      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
  }
  public static String getNowTime(String format)
  {
    return 
      new SimpleDateFormat(format).format(new Date());
  }
  
  public static String getNowDay(String format)
  {
    return 
      new SimpleDateFormat(format).format(new Date());
  }
  
  public static String getYesterday(String format)
  {
    return new SimpleDateFormat(format).format(new Date(new Date().getTime() - 86400000L));
  }
  
  public static String getNextday(String format)
  {
    return new SimpleDateFormat(format).format(new Date(new Date().getTime() + 86400000L));
  }
  
  public static String formatTime(String time, String o, String n)
  {
    try
    {
      return new SimpleDateFormat(n).format(new SimpleDateFormat(o).parse(time));
    }
    catch (ParseException e)
    {
      e.printStackTrace();
    }
    return time;
  }
  
  public static Boolean valiInTimes(String time, String times)
  {
    return Boolean.valueOf(times.indexOf(time) > -1);
  }
  
  public static String findStrTimeFormat(String time)
  {
    String[] timeFormats = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "MM-dd", "MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "MM-dd HH:mm" };
    String rFormat = "";
    for (String tf : timeFormats) {
      try
      {
        String tmp = new SimpleDateFormat(tf).format(new SimpleDateFormat(tf).parse(time));
        if (tmp.equals(time)) {
          return tf;
        }
      }
      catch (Exception localException) {}
    }
    return rFormat;
  }
  
  public static int compare_date(String DATE1, String DATE2, String format)
  {
    DateFormat df = new SimpleDateFormat(format);
    try
    {
      Date dt1 = df.parse(DATE1);
      Date dt2 = df.parse(DATE2);
      if (dt1.getTime() > dt2.getTime()) {
        return 1;
      }
      if (dt1.getTime() < dt2.getTime()) {
        return 2;
      }
      return 0;
    }
    catch (Exception exception)
    {
      exception.printStackTrace();
    }
    return 0;
  }
  
  public static Boolean valiTimeInTimes(String date, String startTime, String endTime, String format)
  {
    if ((compare_date(date, startTime, format) == 1) && (compare_date(date, endTime, format) == 2)) {
      return Boolean.valueOf(true);
    }
    return Boolean.valueOf(false);
  }
  
  @SuppressWarnings("unchecked")
public static String matchDateString(String dateStr)
  {
    try
    {
	List matches = null;
      Pattern p = Pattern.compile("(\\d{1,4}[-|\\/|年|\\.]\\d{1,2}[-|\\/|月|\\.]\\d{1,2}([日|号])?(\\s)*(\\d{1,2}([点|时])?((:)?\\d{1,2}(分)?((:)?\\d{1,2}(秒)?)?)?)?(\\s)*(PM|AM)?)", 10);
      Matcher matcher = p.matcher(dateStr);
      if ((matcher.find()) && (matcher.groupCount() >= 1))
      {
        matches = new ArrayList<>();
        for (int i = 1; i <= matcher.groupCount(); i++)
        {
          String temp = matcher.group(i);
          matches.add(temp);
        }
      }
      else
      {
        matches = Collections.EMPTY_LIST;
      }
      if (matches.size() > 0) {
        return ((String)matches.get(0)).trim();
      }
    }
    catch (Exception e)
    {
      return "";
    }
    return dateStr;
  }
  
  public static int getNowWeek()
  {
    Calendar c = Calendar.getInstance();
    c.setTime(new Date(System.currentTimeMillis()));
    int dayOfWeek = c.get(7) - 1;
    return dayOfWeek;
  }
}
